package com.patMall.service.impl;

import com.patMall.entity.BrowsingHistory;
import com.patMall.entity.Comment;
import com.patMall.entity.Commodity;
import com.patMall.entity.CommodityImg;
import com.patMall.mapper.BrowsingHistoryMapper;
import com.patMall.mapper.CommodityImgMapper;
import com.patMall.mapper.CommodityMapper;
import com.patMall.service.BrowsingHistoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 750909637@qq.com
 * @PackageName patMall
 * @Package com.patMall.service.impl
 * @Date 2024/2/13 9:34
 * @Version 1.0
 */
@Service
public class BrowsingHistoryServiceImpl implements BrowsingHistoryService {
    @Resource
    private BrowsingHistoryMapper browsingHistoryMapper;
    @Resource
    private CommodityMapper commodityMapper;
    @Resource
    private CommodityImgMapper commodityImgMapper;
    @Override
    public int addBrowsingHistory(BrowsingHistory browsingHistory) {
        return browsingHistoryMapper.insertBrowsingHistory(browsingHistory);
    }

    @Override
    public int updateBrowsingHistory(BrowsingHistory browsingHistory) {
        return browsingHistoryMapper.updateBrowsingHistory(browsingHistory);
    }

    @Override
    public BrowsingHistory getBrowsingHistory(Integer commodityId) {
        System.err.println(commodityId);
        return browsingHistoryMapper.selectBrowsingHistory(commodityId);
    }

    @Override
    public List<BrowsingHistory> getBrowsingHistoryCommodity(Integer userId) {
        List<BrowsingHistory> browsingHistoryList = browsingHistoryMapper.selectBrowsingHistoryUser(userId);
        for (BrowsingHistory browsingHistory : browsingHistoryList) {
            Commodity commodity = commodityMapper.getCommodityId(browsingHistory.getCommodityId());
            List<CommodityImg> commodityImg = commodityImgMapper.getAllByCommodity_id(commodity.getCommodityId());
            commodity.setCommodityImgList(commodityImg);
            browsingHistory.setCommodity(commodity);
        }
        return browsingHistoryList;
    }
}
